package ordercode;

import java.util.Arrays;

public class merge {
    public static void main(String[] args) {
        int[] arr = {6,5,34,12,23,67,13};
        mergeSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void mergeSort(int[] arr) {
        if (arr.length > 1) {
            int mid = arr.length / 2;
            int[] left_arr = Arrays.copyOfRange(arr, 0, mid);
            int[] right_arr = Arrays.copyOfRange(arr, mid, arr.length);
            mergeSort(left_arr);
            mergeSort(right_arr);

            int k = 0, i = 0, j = 0;
            while (i < left_arr.length && j < right_arr.length) {
                if (left_arr[i] < right_arr[j]){
                    arr[k] = left_arr[i];
                    i++;
                }else {
                    arr[k] = right_arr[j];
                    j++;
                }
                k++;
            }
            while (i < left_arr.length){
                arr[k] = left_arr[i];
                i++;
                k++;
            }
            while (j < right_arr.length){
                arr[k] = right_arr[j];
                j++;
                k++;
            }
        }
    }
}
